package com.cjjd.c.feature.mine.order;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarActivity;
import com.cjjd.c.bean.CheckOutBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.feature.home.checkout.MyBaseExpandableLisetAdapter;
import com.cjjd.c.feature.home.checkout.OrderCostsAdapter;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.CommonUtil;
import com.cjjd.c.utils.SharePrefUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2017/9/24.
 */

public class OrderDetailActivity extends ActionBarActivity {

    @Bind(R.id.elistview)
    ExpandableListView elistview;
    @Bind(R.id.tv_hotel_name)
    TextView tvHotelName;
    @Bind(R.id.tv_time_for_settle_leave)
    TextView tvTimeForSettleLeave;
    @Bind(R.id.tv_hotel_style)
    TextView tvHotelStyle;
    @Bind(R.id.tv_hotel_depositMoney)
    TextView tvHotelDepositMoney;
    @Bind(R.id.tv_hotel_left_depositMoney)
    TextView tvHotelLeftDepositMoney;
    @Bind(R.id.tv_costMoney)
    TextView tvCostMoney;
    @Bind(R.id.tv_refund_money)
    TextView tvRefundMoney;
    @Bind(R.id.tv_need_pay)
    TextView tvNeedPay;
    @Bind(R.id.tv_receipt_company_name)
    TextView tvReceiptCompanyName;

    @Bind(R.id.jiesuanfangshi)
    TextView tvjiesuanfangshi;

    @Bind(R.id.ll_receipt)
    LinearLayout ll_receipt;
    @Bind(R.id.fapiao)
    TextView tvfapiao;

//    @Bind(R.id.listview_orderCosts)
//    ListView listview_orderCosts;

    private CheckOutBean.Order orderDetailBean;
    private String orderMainUuid;
    private MyBaseExpandableLisetAdapter myBaseExpandableLisetAdapter;

    private OrderCostsAdapter orderCostsAdapter;
    private List<CheckOutBean.Order.OrderCostDetailModel> orderCostDetailModelList = new ArrayList<>();

    @Override
    public Object getTitleString() {
        return "入住单详情";
    }

    @Override
    public int getContentView() {
        return R.layout.activity_order_detail;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ButterKnife.bind(this);

        initView();
        getintentData();

    }

    private void getintentData() {
        orderMainUuid = getIntent().getStringExtra("orderMainUuid");
        if (!TextUtils.isEmpty(orderMainUuid)) {
            getCheckOutOrderDetail();
        }
    }

    private ArrayList<String> gData = null;
    private List<List<CheckOutBean.Order.CheckOutDetail.Costs>> IData = null;


    public void initView() {

        gData = new ArrayList<>();
        IData = new ArrayList<>();
        myBaseExpandableLisetAdapter = new MyBaseExpandableLisetAdapter(mContext);
        myBaseExpandableLisetAdapter.setGroupData(IData);
        myBaseExpandableLisetAdapter.setItemData(gData);
        elistview.setAdapter(myBaseExpandableLisetAdapter);

        orderCostsAdapter = new OrderCostsAdapter(mContext);
        orderCostsAdapter.setData(orderCostDetailModelList);
//        listview_orderCosts.setAdapter(orderCostsAdapter);

    }

    private void getCheckOutOrderDetail() {
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("orderUuid", orderMainUuid);

        Call<HttpResult<CheckOutBean>> call = service.getCustomerChekOutOrderDetail(JsonUtils.convertParam("getCustomerChekOutOrderDetail", h1));
        call.enqueue(new CallbackImpl<CheckOutBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<CheckOutBean>> call, Response<HttpResult<CheckOutBean>> response) {
                if (response.body().getData() != null) {
                    orderDetailBean = response.body().getData().order;
                    ChangeUI(orderDetailBean);
                }
            }
        });
    }

    private void ChangeUI(CheckOutBean.Order orderDetailBean) {
        if (orderDetailBean == null) {
            return;
        }
        tvHotelName.setText(orderDetailBean.hotelName);

        if (orderDetailBean.checkInDate.length() > 10 && orderDetailBean.checkOutDate.length() > 10) {
            tvTimeForSettleLeave.setText("入离店日期：" + orderDetailBean.checkInDate.substring(0, 10) + "至" + orderDetailBean.checkOutDate.substring(0, 10) + "   共" + orderDetailBean.checkInDayNum + "天");
        } else {
            tvTimeForSettleLeave.setText("入离店日期：" + orderDetailBean.checkInDate + "" + orderDetailBean.checkOutDate + "共" + orderDetailBean.checkInDayNum + "天");
        }
        tvHotelStyle.setText(orderDetailBean.roomTypeName + "|" + orderDetailBean.breakfastTypeName);

        // TODO: 2017/9/18 设置多折叠信息
        //ExpandableListView
        if (gData.size() > 0) gData.clear();
        if (IData.size() > 0) IData.clear();
        gData.add("" + orderDetailBean.details.size());
        IData.add(new ArrayList<CheckOutBean.Order.CheckOutDetail.Costs>());
        int checoutNum = orderDetailBean.details.size();
        for (int i = 0; i < checoutNum; i++) {
            gData.add(orderDetailBean.details.get(i).chekInName + ", " + orderDetailBean.details.get(i).costTotalMoney);
            IData.add(orderDetailBean.details.get(i).costs);
        }

////        //TODO dada隐藏 手机号 身份证 三个字段
//        gData.add(orderDetailBean.liveManMobile + "");
//        gData.add(orderDetailBean.liveManIdCard + "");
//        if (orderDetailBean.checkInDate.length() > 10 && orderDetailBean.checkOutDate.length() > 10) {
//            gData.add(orderDetailBean.checkInDate.substring(0, 10) + " 至 " + orderDetailBean.checkOutDate.substring(0, 10));
//        } else {
//            gData.add(orderDetailBean.checkInDate + " - " + orderDetailBean.checkOutDate);
//        }
//        IData.add(new ArrayList<CheckOutBean.Order.CheckOutDetail.Costs>());
//        IData.add(new ArrayList<CheckOutBean.Order.CheckOutDetail.Costs>());
//        IData.add(new ArrayList<CheckOutBean.Order.CheckOutDetail.Costs>());

        myBaseExpandableLisetAdapter.notifyDataSetChanged();


        //region 扫码消费
        if (orderCostDetailModelList.size() > 0) {
            orderCostDetailModelList.clear();
        }
        orderCostDetailModelList.addAll(orderDetailBean.orderCosts);
        if (orderCostDetailModelList.size() > 0) {
            orderCostsAdapter.notifyDataSetChanged();
//            CommonUtil.setListViewHeightBasedOnChildren(listview_orderCosts);
//            listview_orderCosts.setVisibility(View.VISIBLE);
        }

        //endregion


        tvHotelDepositMoney.setText(orderDetailBean.depositMoney + "元");
        tvHotelLeftDepositMoney.setText(orderDetailBean.leftDepositMoney + "元");
        tvCostMoney.setText(orderDetailBean.costMoney + "元");
        tvRefundMoney.setText(orderDetailBean.refundMoney + "元");


        tvjiesuanfangshi.setText(orderDetailBean.refundTypeName);
        if (TextUtils.isEmpty(orderDetailBean.invoiceInfo)) {
            ll_receipt.setVisibility(View.GONE);
        } else {
            ll_receipt.setVisibility(View.VISIBLE);
            tvfapiao.setText(orderDetailBean.invoiceInfo);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }


}
